/*****************************************************************************************
   
   Copyright (c) Microsoft Corporation. All rights reserved.

   Use of this code sample is subject to the terms of the Microsoft
   Permissive License, a copy of which should always be distributed with
   this file.  You can also access a copy of this license agreement at:
   http://www.microsoft.com/resources/sharedsource/licensingbasics/permissivelicense.mspx

 ****************************************************************************************/

using System;

namespace GrooveWebServicesV12Helpers.Forms
{
	/// <summary>
	/// Represents schema and/or data in a FormsTool.
	/// </summary>
	public class RecordDataSet
	{
		private GrooveWebServicesV12Helpers.GrooveFormsWebService.FormsRecordDataSet m_FormsRecordDataSet;

		public RecordDataSet(GrooveWebServicesV12Helpers.GrooveFormsWebService.FormsRecordDataSet i_FormsRecordDataSet)
		{
			m_FormsRecordDataSet = i_FormsRecordDataSet;

		}


		/// <summary>
		/// Construct from a System.Data.DataSet.
		/// </summary>
		/// <param name="i_DataSet">DataSet</param>
		public RecordDataSet(System.Data.DataSet i_DataSet)
		{
			m_FormsRecordDataSet = new GrooveWebServicesV12Helpers.GrooveFormsWebService.FormsRecordDataSet();
			
			//  Translate the schema
			System.Xml.XmlDocument XmlDocument = new System.Xml.XmlDocument();
			string SchemaXML = i_DataSet.GetXmlSchema();
			XmlDocument.LoadXml(SchemaXML);
			m_FormsRecordDataSet.Schema = new System.Xml.XmlNode[]
				{
					XmlDocument.DocumentElement
				};

			//  Translate the data:
			string DataXML = i_DataSet.GetXml();
			XmlDocument.LoadXml(DataXML);
			if (XmlDocument.DocumentElement.HasChildNodes) 
			{
				int ChildNodesLength = XmlDocument.DocumentElement.ChildNodes.Count;
				System.Xml.XmlNode[] DataNodes = new System.Xml.XmlNode[ChildNodesLength];
				for (int Index = 0; Index < ChildNodesLength; ++Index) 
				{
					DataNodes[Index] = XmlDocument.DocumentElement.ChildNodes[Index];
				}
				m_FormsRecordDataSet.Data = DataNodes;
			} 
			else 
			{
				m_FormsRecordDataSet.Data = new object();
			}
		}

		/// <summary>
		/// Construct from XML serialized schema and data elements.
		/// </summary>
		/// <param name="i_SchemaNodes">XmlNode array containing the schema.</param>
		/// <param name="i_DataNodes">XmlNode array containing the data.</param>
		public RecordDataSet(System.Xml.XmlNode[] i_SchemaNodes, System.Xml.XmlNode[] i_DataNodes)
		{
			m_FormsRecordDataSet = new GrooveFormsWebService.FormsRecordDataSet();
			m_FormsRecordDataSet.Schema = i_SchemaNodes;
			m_FormsRecordDataSet.Data = i_DataNodes;
		}

		/// <summary>
		/// Get serialized XmlNode array containing the data.
		/// </summary>
		public System.Xml.XmlNode[] Data
		{
			get
			{
				if (m_FormsRecordDataSet.Data is System.Array)
					return (System.Xml.XmlNode[])m_FormsRecordDataSet.Data;

				return new System.Xml.XmlNode[0];
			}
		}

		/// <summary>
		/// Get serialized XmlNode array containing the schema.
		/// </summary>
		public System.Xml.XmlNode[] Schema
		{
			get
			{
				if (m_FormsRecordDataSet.Schema is System.Array)
					return (System.Xml.XmlNode[])m_FormsRecordDataSet.Schema;

				return new System.Xml.XmlNode[0];
			}
		}

		/// <summary>
		/// Get the version of the schema.
		/// </summary>
		public string SchemaVersionID
		{
			get
			{
				return m_FormsRecordDataSet.SchemaVersionID;
			}
		}

		/// <summary>
		/// Convert to a System.Data.DataSet
		/// </summary>
		/// <returns>DataSet</returns>
		public System.Data.DataSet ToDataSet()
		{
			System.Data.DataSet DataSet = new System.Data.DataSet("RecordDataSet");

			//  If schema exists, translate it
			if (this.Schema.Length > 0)
			{
				System.Xml.XmlNode SchemaNode = this.Schema[0];
				System.IO.StringReader SchemaReader = new System.IO.StringReader("<?xml version=\"1.0\"?>" + SchemaNode.OuterXml);
				DataSet.ReadXmlSchema(SchemaReader);
			}

			// If data exists, translate it
			if (this.Data.Length > 0)
			{
				foreach(System.Xml.XmlNode DataNode in this.Data)
				{
					System.Xml.XmlNodeReader DataReader = new System.Xml.XmlNodeReader(DataNode);
					DataSet.ReadXml
						(
						DataReader, 
						System.Data.XmlReadMode.Auto
						);
				}
			}

			return DataSet;
		}
	}
}
